package demo2;

/**
 * @Author kiku_wa
 * @Description:
 */

/**
 * <T>表示 当前类是一个泛型类
 * @param <T>
 */
class MyArray<T>{
    public Object[] arr = new Object[3];
    public T getVal(int pos) {
        return (T) arr[pos];
    }

    public void setArr(int pos,T val) {
       arr[pos] = val;
    }
}
public class Test1 {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<Integer>();
        myArray.setArr(0,88);
        //myArray.setArr(1,"well");报错，编译时自动进行类型检查
        myArray.setArr(1,99);
        Integer ret =myArray.getVal(0);//编译时，自动进行类型转换
        System.out.println(ret);

        MyArray<String> myArray2 = new MyArray<>();
        myArray2.setArr(0,"good");
        myArray2.setArr(1,"body");
        String ret2 = myArray2.getVal(1);
        System.out.println(ret2);
    }
    public static void main1(String[] args) {
        MyArray myArray = new MyArray();
        myArray.setArr(0,6);
        myArray.setArr(1,"happy");
        String ret = (String)myArray.getVal(1);
        System.out.println(ret);
    }
}
